package com.earl.spider.common.onlinecourse;

import com.earl.spider.entity.po.UserInfo;
import com.earl.spider.util.SpiderUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.util.StringUtils;

@Slf4j
public class OnlineCourseInit {

    public static boolean login(WebDriver driver, UserInfo userInfo) throws Exception {
        SpiderUtil.requestData(userInfo.getBaseUrl() + "loginController.do?login", driver);
        WebElement userName = SpiderUtil.getElement(By.id("userName"), driver);
        if (StringUtils.isEmpty(userName)) {
            throw new RuntimeException("未找见用户名输入框");
        }
        userName.sendKeys(userInfo.getUsername());
        WebElement password = SpiderUtil.getElement(By.id("password"), driver);
        if (StringUtils.isEmpty(password)) {
            throw new RuntimeException("未找见密码输入框");
        }
        password.sendKeys(userInfo.getPassword());
        WebElement butLogin = SpiderUtil.getElement(By.id("but_login"), driver);
        if (StringUtils.isEmpty(butLogin)) {
            throw new RuntimeException("未找见登陆按钮");
        }
        SpiderUtil.elementJsClick(butLogin, driver);
        return true;

    }
}
